<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <h2>系统登录</h2>
      </template>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="rules"
        label-width="0"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            :type="passwordVisible ? 'text' : 'password'"
            placeholder="密码"
            :prefix-icon="Lock"
          >
            <template #suffix>
              <el-icon
                class="cursor-pointer"
                @click="passwordVisible = !passwordVisible"
              >
                <component :is="passwordVisible ? View : Hide" />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="w-full"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, View, Hide } from '@element-plus/icons-vue'
import request from '@/utils/request'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()
const loginFormRef = ref(null)
const loading = ref(false)
const passwordVisible = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    console.log('Sending login request...');
    const response = await request({
      url: '/login',
      method: 'POST',
      data: {
        username: loginForm.username,
        password: loginForm.password
      }
    })
    
    console.log('Login response:', response);
    
    if (response.code === 200) {
      // 保存登录状态和用户信息
      authStore.setToken(response.data.token)
      authStore.setUser(response.data.user)
      
      ElMessage.success('登录成功')
      router.push('/activities')
    } else {
      ElMessage.error(response.message || '登录失败')
    }
  } catch (error) {
    console.error('Login failed:', {
      message: error.message,
      code: error.code,
      response: error.response,
      config: error.config
    });
    
    if (error.code === 'ECONNABORTED') {
      ElMessage.error('登录请求超时，请检查网络连接')
    } else {
      ElMessage.error(error.response?.data?.message || '登录失败，请稍后重试')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.login-card {
  width: 100%;
  max-width: 400px;
}

.login-card :deep(.el-card__header) {
  text-align: center;
  padding: 20px;
}

.login-card h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.w-full {
  width: 100%;
}

.cursor-pointer {
  cursor: pointer;
}
</style> 